package com.xpgk.mvc.binShopService;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productOrgExperience.ProductOrgExperience;
import com.xpgk.mvc.statistic.daySales.DaySalesStatisticSQL;

public class TodayServedController extends  BaseController{
	
	private BinShopServiceService binShopServiceService = new BinShopServiceService();
	public void index(){
		List<Record> result = binShopServiceService.getTodayView(getCUserOrganIds(), null, null, null,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		Record record = binShopServiceService.totaloftodayservice(getCUserOrganIds(),new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		setAttr("resultTotal",record);
		
		String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
		String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
		String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
		ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,getCUserOrganIds());
		int n = 4;
		if(bb!=null){
			n++;
			setAttr("bbExp", "1");
		}else{
			setAttr("bbExp", "0");
		}
		ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,getCUserOrganIds());
		if(kb!=null){
			n++;
			setAttr("kbExp", "1");
		}else{
			setAttr("kbExp", "0");
		}
		ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,getCUserOrganIds());
		if(fr!=null){
			n = n+2;
			setAttr("Exp", "1");
		}else{
			setAttr("Exp", "0");
		}
		setAttr("partnum",n);
		setAttr("resultList", result);
		//setAttr("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		render("todayServices.html");
	}
	
	public void view() throws UnsupportedEncodingException, ParseException {
		String name = getPara("name");
		String dateStr = getPara("date");
		if(dateStr == null || dateStr.equals("")){
			dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		}
		List<Record> result = binShopServiceService.getTodayView(getCUserOrganIds(),name , getPara("phone"),getPara("hybh"),dateStr);
		Record record = binShopServiceService.totaloftodayservice(getCUserOrganIds(),dateStr);
		setAttr("resultTotal",record);
		setAttr("resultList", result);
		setAttr("customerName",name);
		setAttr("phone", getPara("phone"));
		setAttr("customerHybh", getPara("hybh"));
		setAttr("date", dateStr);
		String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
		String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
		String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
		ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,getCUserOrganIds());
		int n = 4;
		if(bb!=null){
			n++;
			setAttr("bbExp", "1");
		}else{
			setAttr("bbExp", "0");
		}
		ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,getCUserOrganIds());
		if(kb!=null){
			n++;
			setAttr("kbExp", "1");
		}else{
			setAttr("kbExp", "0");
		}
		ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,getCUserOrganIds());
		if(fr!=null){
			n = n+2;
			setAttr("Exp", "1");
		}else{
			setAttr("Exp", "0");
		}
		setAttr("partnum",n);
		render("todayServices.html");
		
	}
	
	public void queryDaily(){
		List<Record> result = binShopServiceService.getTodayView(getPara("organids"), null, null, null,new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		Record record = binShopServiceService.totaloftodayservice(getPara("organids"),new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		setAttr("resultTotal",record);
		
		String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
		String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
		String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
		ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,getPara("organids"));
		int n = 4;
		if(bb!=null){
			n++;
			setAttr("bbExp", "1");
		}else{
			setAttr("bbExp", "0");
		}
		ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,getPara("organids"));
		if(kb!=null){
			n++;
			setAttr("kbExp", "1");
		}else{
			setAttr("kbExp", "0");
		}
		ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,getPara("organids"));
		if(fr!=null){
			n = n+2;
			setAttr("Exp", "1");
		}else{
			setAttr("Exp", "0");
		}
		setAttr("organids", getPara("organids"));
		setAttr("partnum",n);
		setAttr("resultList", result);
		//setAttr("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		render("/dlstasticinfo/dltodayServices.html");
	}
	
	public void dlview() throws UnsupportedEncodingException, ParseException {
		String organids = getPara("organids");
		String dateStr = getPara("date");
		if(dateStr == null || dateStr.equals("")){
			dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		}
		List<Record> result = binShopServiceService.dlgetTodayView(organids ,dateStr);
		Record record = binShopServiceService.totaloftodayservice(organids,dateStr);
		setAttr("resultTotal",record);
		setAttr("resultList", result);
		
		setAttr("date", dateStr);
		String sqlbb = "SELECT * FROM sb_product_orgexperience where organIds =? and bbExperience IS NOT NULL and dataStatus = '1'";
		String sqlkb = "SELECT * FROM sb_product_orgexperience where organIds =? and kbExperience IS NOT NULL and dataStatus = '1'";
		String sqlfr = "SELECT * FROM sb_product_orgexperience where organIds =? and frExperience IS NOT NULL and dataStatus = '1'";
		ProductOrgExperience bb = ProductOrgExperience.dao.findFirst(sqlbb,getCUserOrganIds());
		int n = 4;
		if(bb!=null){
			n++;
			setAttr("bbExp", "1");
		}else{
			setAttr("bbExp", "0");
		}
		ProductOrgExperience kb = ProductOrgExperience.dao.findFirst(sqlkb,getCUserOrganIds());
		if(kb!=null){
			n++;
			setAttr("kbExp", "1");
		}else{
			setAttr("kbExp", "0");
		}
		ProductOrgExperience fr = ProductOrgExperience.dao.findFirst(sqlfr,getCUserOrganIds());
		if(fr!=null){
			n = n+2;
			setAttr("Exp", "1");
		}else{
			setAttr("Exp", "0");
		}
		setAttr("partnum",n);
		render("/dlstasticinfo/dltodayServices.html");
		
	}
	
	
//	public void dateView() throws UnsupportedEncodingException, ParseException {
//		String dateStr = getPara("date");
//		if(dateStr == null || dateStr.equals("")){
//			dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//		}
//		List<Record> result = binShopServiceService.getDateView(getCUserOrganIds(),dateStr);
//		setAttr("resultList", result);
//		setAttr("date", dateStr);
//		render("todayServices.html");
//	}
}
